<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <link rel="apple-touch-icon" sizes="76x76" href="/img/apple-icon.png">
    <link rel="icon" type="image/png" href="/img/favicon.png">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <title>
        AntRpc Console
    </title>
    <meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, shrink-to-fit=no'
          name='viewport'/>
    <!--     Fonts and icons     -->
    <link href="/css/google-fonts.css" rel="stylesheet"/>
    <link rel="stylesheet" href="/css/fontawesome-v5.7.1-all.css">
    <!-- CSS Files -->
    <link href="/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="/css/now-ui-dashboard.min.css" rel="stylesheet"/>
    <link href="/css/my.css" rel="stylesheet"/>
    <!-- CSS Just for demo purpose, don't include it in your project -->

</head>
<body>
<div class="wrapper ">
    <div th:replace="~{common :: #common-sidebar}"></div>
    <div class="main-panel" id="main-panel">
        <nav id="common-nav" class="navbar navbar-expand-lg navbar-transparent  bg-primary  navbar-absolute">
            <div class="container-fluid my-padding-left-top-0">
                <div th:replace="~{common :: #commons-nav-btns}"></div>
                <div class="collapse navbar-collapse justify-content-end w-100" id="navigation">
                    <form style="width: 100%;" action="/">
                        <div class="input-group no-border">
                            <input type="text" name="name" class="form-control" placeholder="Search..."/>
                            <div class="input-group-append">
                                <div class="input-group-text">
                                    <i class="now-ui-icons ui-1_zoom-bold"></i>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </nav>
        <div class="panel-header panel-header-sm"></div>
        <div class="content">
            <div class="row">
                <div class="col-md-12">
                    <ol class="breadcrumb my-breadcrumb">
                        <li class="breadcrumb-item"><a href="/">服务列表</a></li>
                        <li class="breadcrumb-item">
                            <a id="class_name_elem" th:href="@{'/methods?className=' + ${className}}"
                               th:text="${className}"></a>
                        </li>
                        <li class="breadcrumb-item active" id="method_name_elem" th:text="${humanFullMethodName}"></li>
                    </ol>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-header">
                            <h5 class="card-title">
                                服务提供方限流
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table">
                                    <thead class="text-primary">
                                    <tr>
                                        <th>#</th>
                                        <th>提供方</th>
                                        <th>限流(次/秒)</th>
                                        <th></th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr th:each="p, itStat: ${providers}">
                                        <td th:text="${itStat.index + 1}"></td>
                                        <td th:text="${p.appNameAndHostInfo}"></td>
                                        <td th:text="${p.limit + '/' + p.durationInSeconds}"></td>
                                        <td>
                                            <button type="button" rel="tooltip" title="修改限流信息"
                                                    class="btn btn-info btn-icon btn-sm "
                                                    th:attr="data-limit=${p.limit}, data-durationInSeconds=${p.durationInSeconds}, data-className=${className}, data-fullName=${humanFullMethodName}, data-appname-host=${p.appNameAndHostInfo}"
                                                    onclick="update_provider_limit(this)">
                                                <i class="now-ui-icons design-2_ruler-pencil"></i>
                                            </button>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-header">
                            <h5 class="card-title">
                                服务消费方限流
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table">
                                    <thead class="text-primary">
                                    <tr>
                                        <th>#</th>
                                        <th>消费方</th>
                                        <th>限流(次/秒)</th>
                                        <th></th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr th:each="p, itStat: ${consumers}">
                                        <td th:text="${itStat.index + 1}"></td>
                                        <td th:text="${p.appNameAndHostInfo}"></td>
                                        <td th:text="${p.limit + '/' + p.durationInSeconds}"></td>
                                        <td>
                                            <button type="button" rel="tooltip" title="修改限流信息"
                                                    class="btn btn-info btn-icon btn-sm "
                                                    th:attr="data-limit=${p.limit}, data-durationInSeconds=${p.durationInSeconds}, data-className=${className}, data-fullName=${humanFullMethodName}, data-appname-host=${p.appNameAndHostInfo}"
                                                    onclick="update_consumer_limit(this)">
                                                <i class="now-ui-icons design-2_ruler-pencil"></i>
                                            </button>
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="limit_data_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-scrollable">
        <div class="modal-content">
            <div class="modal-header justify-content-center">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    <i class="now-ui-icons ui-1_simple-remove"></i>
                </button>
                <h4 class="title title-up my-not-uppercase" id="app_name_host_elem"></h4>
            </div>
            <div class="modal-body">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-12">
                            <input type="hidden" id="modal_class_elem"/>
                            <input type="hidden" id="modal_full_method_elem"/>
                            <input type="hidden" id="modal_provider_or_consumer_elem"/>
                            <div class="form-group">
                                <label for="limit_elem">次数:</label>
                                <input type="text" class="form-control" id="limit_elem"/>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="seconds_elem">秒:</label>
                                <input type="text" class="form-control" id="seconds_elem"/>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="save_method_limit();">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

<div th:insert="js"></div>
<script>

    function save_method_limit() {
        var limit = $.trim($("#limit_elem").val());
        if (!/^\d+$/.test(limit)) {
            antrpc_console.showNotification("次数必须是整数", {
                color: "danger"
            });
            return;
        }
        var durationInSeconds = $.trim($("#seconds_elem").val());
        if (!/^\d+$/.test(durationInSeconds)) {
            antrpc_console.showNotification("秒必须是整数", {
                color: "danger"
            });
            return;
        }
        var fullMethodName = $("#modal_full_method_elem").val();
        var className = $("#modal_class_elem").val();
        var appHostInfo = $("#app_name_host_elem").text();
        var type = $("#modal_provider_or_consumer_elem").val();
        $.ajax({
            url: "/interface/updateRateLimit",
            type: "POST",
            data: {
                "className": className,
                "fullMethodName": fullMethodName,
                "limit": limit,
                "durationInSeconds": durationInSeconds,
                "appHostInfo": appHostInfo,
                "type": type
            },
            dataType: "JSON",
            success: function (response) {
                if (response.code === 2000) {
                    antrpc_console.showNotification("修改成功!");
                } else {
                    antrpc_console.showNotification(response.message, {
                        "color": "danger"
                    });
                }
                window.setTimeout(function () {
                    window.location.reload();
                }, 1000);
            }
        });
    }

    function update_consumer_limit(btn) {
        internal_init_modal(btn);
        $("#modal_provider_or_consumer_elem").val("consumer");
        $("#limit_data_modal").modal("show");
    }

    function update_provider_limit(btn) {
        internal_init_modal(btn);
        $("#modal_provider_or_consumer_elem").val("provider");
        $("#limit_data_modal").modal("show");
    }

    function internal_init_modal(btn) {
        $("#app_name_host_elem").text($(btn).attr("data-appname-host"));
        $("#modal_class_elem").val($(btn).attr("data-className"));
        $("#modal_full_method_elem").val($(btn).attr("data-fullName"));
        $("#limit_elem").val($(btn).attr("data-limit"));
        $("#seconds_elem").val($(btn).attr("data-durationInSeconds"));
    }

    $(document).ready(function () {
        $("#service_list_li").addClass("active");

        $("#limit_data_modal").on("hidden.bs.modal", function () {
            $("#app_name_host_elem").text("");
            $("#modal_class_elem").val("");
            $("#modal_full_method_elem").val("");
            $("#limit_elem").val("");
            $("#seconds_elem").val();
        });
    });


</script>
</body>
</html>